﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_PositionDutyDtls : System.Web.UI.UserControl
    {
        private IPositionBLL posBLL = new PositionBLL();
        private IPositionDutyBLL dutyBLL = new PositionDutyBLL();
        private IPositionDutyDtlsBLL dtlsBLL = new PositionDutyDtlsBLL();

        private PositionDutyDtlsInfo posDtlsInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadHeader();
            FillDataToDuty();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSeq.Text = DataBinder.Eval(DataItem, "Seq").ToString();
                    cmbDuty.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "DutySeq"));
                    txtNo.Text = DataBinder.Eval(DataItem, "No").ToString();
                    txtDutyDescr.Text = DataBinder.Eval(DataItem, "Descr").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void LoadHeader()
        {
            IList<PositionInfo> posList = posBLL.ListPositions(Session["PosSeq"].ToString());
            if (posList.Count > 0)
            {
                lblPosTypeName.Text = posList[0].PositionType.Name;
               // lblFieldName.Text = posList[0].Field.Name;
                lblPosName.Text = posList[0].Name;
            }
        }

        public void FillDataToDuty()
        {
            IDutyBLL dutyBLL = new DutyBLL();
            IList<DutyInfo> info = dutyBLL.ListDuties(null);

            cmbDuty.DataSource = info;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            posDtlsInfo = new PositionDutyDtlsInfo();
            Nullable<int> posSeq = Session["PosSeq"].ToString().Length > 0 ? Int32.Parse(Session["PosSeq"].ToString()) : (int?)null;

            posDtlsInfo.PositionSeq = posSeq;
            posDtlsInfo.DutySeq = cmbDuty.SelectedValue.Length > 0 ? Int32.Parse(cmbDuty.SelectedValue) : (int?)null;
            posDtlsInfo.No = txtNo.Text.Trim();
            posDtlsInfo.Descr = txtDutyDescr.Text;
            posDtlsInfo.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            posDtlsInfo.CreatedBy = 1;

            dtlsBLL.Insert(posDtlsInfo);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            posDtlsInfo = new PositionDutyDtlsInfo();

            posDtlsInfo.Seq = lblSeq.Text.Length > 0 ? Int32.Parse(lblSeq.Text) : (int?)null;
            posDtlsInfo.DutySeq = cmbDuty.SelectedValue.Length > 0 ? Int32.Parse(cmbDuty.SelectedValue) : (int?)null;
            posDtlsInfo.No = txtNo.Text.Trim();
            posDtlsInfo.Descr = txtDutyDescr.Text;
            posDtlsInfo.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            posDtlsInfo.UpdatedBy = 1;

            dtlsBLL.Update(posDtlsInfo);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

    }
}